<?php
/**
 * Created by PhpStorm.
 * User: wangchaozhen
 * Date: 2018/6/1
 * Time: 21:20
 */

namespace App\Http\Controllers\Cart;

use App\Common\Result;
use App\Http\Controllers\Controller;
use App\Service\Cart\CartService;
use App\Service\Goods\GoodsService;
use Illuminate\Http\Request;

class CartController extends Controller
{
    /**
     * 放入购物车
     */
    public function putIntoCart(Request $request)
    {
        $goods_id = $request->goods_id;

        $buy_uid = $request->buy_uid;

        $number = $request->number;

        $goods_info = GoodsService::getGoodsIdInfo($goods_id);


        //过滤信息
        if(!$goods_info)
            Result::msg('-45','error','无此商品信息');

        if($goods_info['stock']<1)
            Result::msg('-46','error','无此库存');

        if($goods_info['is_up'] !=1)
            Result::msg('-44','error','商品已经下架');

        if($number<1)
            Result::msg('-47','error','数量不能小于1');


        try{

            $exist_cart = CartService::getCartInfo($goods_id,$buy_uid);
            if($exist_cart){
                $result = CartService::changeCartNums($exist_cart['id'],$buy_uid,$number+$exist_cart['number']);
            }else{
                $result = CartService::intoCart($goods_id,$buy_uid,$number);
            }


            if($result){
                Result::msg('0','success',[]);
            }else {
                Result::msg('-48','error','加入购物车失败');
            }
        }catch (\Exception $e){

            Result::msg('-48','error','加入购物车失败');
        }
    }

    /**
     * 我的购物车列表
     */
    public function cartList(Request $request)
    {
        $uid = $request->uid;

        $result = CartService::getList($uid);

        Result::msg('0','success',$result);
    }

    /**
     * 删除购物车中商品
     */
    public function delSomeCart(Request $request)
    {
        $cart_ids = $request->cart_ids;
        $uid = $request->uid;

        $cart_ids = explode(',',$cart_ids);
        if(!$cart_ids){
            Result::msg('-49','error','请选择商品!');
        }

        try{
            $result = CartService::delCarts($cart_ids,$uid);

            if($result){
                Result::msg('0','success',[]);
            }else {
                Result::msg('-50','error','删除购物车失败');
            }
        }catch (\Exception $e){
            Result::msg('-50','error','删除购物车失败');
        }
    }

    /**
     * 清空购物车
     */
    public function delAllCart(Request $request)
    {
        $uid = $request->uid;

        try{
            $result = CartService::emptyCarts($uid);

            if($result){
                Result::msg('0','success',[]);
            }else {
                Result::msg('-50','error','删除购物车失败');
            }
        }catch (\Exception $e){
            Result::msg('-50','error','删除购物车失败');
        }
    }

    /**
     * 改版购物车商品数量
     */
    public function changeCartNum(Request $request)
    {
        $cart_id = $request->cart_id;
        $uid = $request->uid;
        $number = $request->number;

        $cart_goods_info = CartService::getCartGoodsInfo($cart_id,$uid);

        if(!$cart_goods_info)
            Result::msg('-45','error','无此商品信息');

        if($cart_goods_info['stock']<1)
            Result::msg('-46','error','无此库存');

        if($cart_goods_info['is_up'] !=1)
            Result::msg('-44','error','商品已经下架');

        if($number<1)
            Result::msg('-47','error','数量不能小于1');

        try{
            CartService::changeCartNums($cart_id,$uid,$number);
            Result::msg('0','success',[]);
        }catch (\Exception $e){
            Result::msg('-51','error','添加失败');
        }
    }
}